package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.system.domain.TbOrder;
import com.ruoyi.system.service.ITbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbFurnitureMapper;
import com.ruoyi.system.domain.TbFurniture;
import com.ruoyi.system.service.ITbFurnitureService;


/**
 * 家具管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class TbFurnitureServiceImpl implements ITbFurnitureService 
{
    @Autowired
    private TbFurnitureMapper tbFurnitureMapper;

    @Autowired
    private ITbOrderService tbOrderService;

    /**
     * 查询家具管理
     * 
     * @param furnitureId 家具管理主键
     * @return 家具管理
     */
    @Override
    public TbFurniture selectTbFurnitureByFurnitureId(Long furnitureId)
    {
        return tbFurnitureMapper.selectTbFurnitureByFurnitureId(furnitureId);
    }

    /**
     * 查询家具管理列表
     * 
     * @param tbFurniture 家具管理
     * @return 家具管理
     */
    @Override
    public List<TbFurniture> selectTbFurnitureList(TbFurniture tbFurniture)
    {
        return tbFurnitureMapper.selectTbFurnitureList(tbFurniture);
    }

    /**
     * 新增家具管理
     * 
     * @param tbFurniture 家具管理
     * @return 结果
     */
    @Override
    public int insertTbFurniture(TbFurniture tbFurniture)
    {
        return tbFurnitureMapper.insertTbFurniture(tbFurniture);
    }

    /**
     * 修改家具管理
     * 
     * @param tbFurniture 家具管理
     * @return 结果
     */
    @Override
    public int updateTbFurniture(TbFurniture tbFurniture)
    {
        // 检查库存是否为0，如果是，则将isAvailable设置为1（不可出租）
        if (tbFurniture.getStock() != null && tbFurniture.getStock() == 0) {
            tbFurniture.setIsAvailable(1);
        }
        return tbFurnitureMapper.updateTbFurniture(tbFurniture);
    }

    /**
     * 批量删除家具管理
     * 
     * @param furnitureIds 需要删除的家具管理主键
     * @return 结果
     */
    @Override
    public int deleteTbFurnitureByFurnitureIds(Long[] furnitureIds)
    {
        return tbFurnitureMapper.deleteTbFurnitureByFurnitureIds(furnitureIds);
    }

    /**
     * 删除家具管理信息
     * 
     * @param furnitureId 家具管理主键
     * @return 结果
     */
    @Override
    public int deleteTbFurnitureByFurnitureId(Long furnitureId)
    {
        return tbFurnitureMapper.deleteTbFurnitureByFurnitureId(furnitureId);
    }

    /**
     * 出租家具
     */
    @Override
    public int rentFurniture(TbFurniture tbFurniture)
    {
        // 获取当前库存
        TbFurniture currentFurniture = tbFurnitureMapper.selectTbFurnitureByFurnitureId(tbFurniture.getFurnitureId());
        int currentStock = currentFurniture.getStock().intValue();

        // 计算新的库存
        int newStock = currentStock - tbFurniture.getRentQuantity();
        if (newStock < 0) {
            throw new RuntimeException("库存不足");
        }

        // 更新库存
        currentFurniture.setStock((long) newStock);

        // 更新租赁信息
        currentFurniture.setDepositRequirement(tbFurniture.getDepositRequirement());
        currentFurniture.setRentalPrice(tbFurniture.getRentalPrice());

        // 如果库存为0，将可租赁状态设置为不可出租
        if (newStock == 0) {
            currentFurniture.setIsAvailable(1);
        }

        // 插入订单信息
        TbOrder tbOrder = new TbOrder();
        tbOrder.setUserId(tbFurniture.getUserId()); // 假设TbFurniture中有userId字段，如果没有需要从其他地方获取
        tbOrder.setOrderNo(generateOrderNo()); // 生成订单编号的方法，需要实现
        tbOrder.setOrderTime(new Date());
        tbOrder.setRentalStartDate(tbFurniture.getRentalStartDate()); // 设置租赁开始时间
        tbOrder.setRentalEndDate(tbFurniture.getRentalEndDate()); // 设置租赁结束时间
        // 计算总金额并格式化为小数点后两位
        BigDecimal totalAmount = tbFurniture.getDepositRequirement().add(tbFurniture.getRentalPrice().multiply(new BigDecimal(tbFurniture.getRentQuantity())));
        tbOrder.setTotalAmount(totalAmount.setScale(2, RoundingMode.HALF_UP));
        tbOrder.setDepositAmount(tbFurniture.getDepositRequirement());
        tbOrder.setDeliveryFee(tbFurniture.getDeliveryFee());
        tbOrder.setDeliveryAddress(tbFurniture.getDeliveryAddress());
        tbOrder.setPaymentMethod(tbFurniture.getPaymentMethod());
        tbOrder.setPaymentTime(new Date()); // 假设支付时间就是当前时间
        tbOrder.setOrderStatus(0L); // 假设1表示订单已创建，需要根据实际情况调整
        tbOrder.setRemark(tbFurniture.getUserRemark());

        tbOrderService.insertTbOrder(tbOrder);

        return tbFurnitureMapper.updateTbFurniture(currentFurniture);
    }

    private String generateOrderNo() {
        // 实现订单编号生成逻辑，例如使用UUID或数据库自增ID
        return UUID.randomUUID().toString();
    }
}
